package gof.com.yew.memento;

import java.util.ArrayList;
import java.util.List;

public class CareTaker {
	private int cursor = 0;
	private List<Memento> ms = new ArrayList<>();
	
	//添加快照
	public void  addList(Memento m) {
		if(cursor == 0) {
			ms.add(new Memento(""));
		}
		ms.add(m);
		cursor++;
	}
	
	//恢复上个快照
	public Memento  revertBack() {
//		System.out.println("cursor =" + cursor);
		if(cursor == 0) {
			return ms.get(0);
		}
		return ms.get(--cursor);
	}
	
	//撤销上个快照
	public Memento  revokeNext() {
//		System.out.println("cursor =" + cursor);
		if(cursor<ms.size()-1) {
			return ms.get(++cursor);
		}else {
			return ms.get(ms.size()-1);
		}
	}
	
	@Override
	public String toString() {
		return "CareTaker [ms=" + ms + "]";
	}
}
